package com.example.intelligentattendancesystem.service.impl;

import com.example.intelligentattendancesystem.Utils.Utile;
import com.example.intelligentattendancesystem.mapping.LeaveMapping;
import com.example.intelligentattendancesystem.mapping.MoreMapping;
import com.example.intelligentattendancesystem.mapping.UserMapping;
import com.example.intelligentattendancesystem.mapping.WorkMapping;
import com.example.intelligentattendancesystem.pojo.Leave;
import com.example.intelligentattendancesystem.pojo.More;
import com.example.intelligentattendancesystem.pojo.User;
import com.example.intelligentattendancesystem.pojo.Work;
import com.example.intelligentattendancesystem.service.LeaveService;
import com.example.intelligentattendancesystem.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author
 * @version 1.0
 * @className LeaveServiceImpl
 * @since 1.0
 */
@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    LeaveMapping leaveMapping;
    @Autowired
    Utile utile;
    @Autowired
    UserMapping userMapping;
    @Autowired
    WorkService workService;
    @Autowired
    WorkMapping workMapping;
    @Autowired
    MoreMapping moreMapping;

    @Override
    public List<Leave> getLeaveService(int userId) {
        List<Leave> leaves = leaveMapping.selectLeaveByUserId(userId);
        return leaves;
    }

    @Override
    public int addLeavesService(List<Leave> leaves) {
        List<Leave> noReviewLeave = utile.getNoReviewLeave(leaves);
        List<Leave> leaves1 = utile.initLeaves(noReviewLeave);
        int i=0;
        for (Leave leave :
                leaves1) {
            i += leaveMapping.insertLeave(leave);
        }
        return i;
    }

    @Override
    public int modifyLeaves(List<Leave> leaves) {
        int i=0,flg=0;
        Leave leave1 = new Leave();
        for (Leave leave : leaves) {
            i += leaveMapping.updateLeave(leave);
            if("yes".equals(leave.getAccess())){
                flg=1;
                leave1=leave;
                break;
            }
        }
        //有通过的请假记录
        if(flg==1){
            //有人请假，智能修改班次
            //若有人请假，就将请假的人移除，然后查询这一周上班次数最少的人来替代
            try {
                smartModefyWorkTime(leave1);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        return i;
    }

    private void smartModefyWorkTime(Leave leave1) throws ParseException {
        //查询这周上班次数最少的人
        //获取本周所有的工作表，每个人的，不算管理员和已经请假的
        List<Work> tableService = workService.getTableService(11);
        int[] nums = new int[1000];
        //，不能算在那个时间段有工作的,排除掉在那个时间段有工作的人的work
        for (int i = 0; i < tableService.size(); i++) {
            if(tableService.get(i).getUserId()==leave1.getUserId()||tableService.get(i).getUserId()==11||leave1.getLeaveStartTime().split(" ")[1].equals(tableService.get(i).getStartTime())){
                tableService.remove(tableService.get(i));
                i--;
            }
        }
        for(Work work:tableService){
            //不能算自己
            if(work.getUserId()!=leave1.getUserId()){
                nums[work.getUserId()]++;
            }
        }
        //获取所有的员工
        List<User> users = userMapping.selectAllUser();
        for (int i = 0; i < users.size(); i++) {
            //不能算自己
            if(users.get(i).getUserId()!=leave1.getUserId()){
                nums[users.get(i).getUserId()]++;
            }
        }
        nums[leave1.getUserId()]=0;
        nums[11]=0;
        //获取上班最少次数的人的id和次数
        int id = tableService.get(0).getUserId(),min=nums[tableService.get(0).getUserId()];
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]!=0&&nums[i]<min){
                id=i;
                min=nums[i];
            }
        }

        //获取周
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = df.parse(leave1.getLeaveStartTime());
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(parse);
        Integer week= calendar.get(Calendar.DAY_OF_WEEK)-1;
        //替换掉请假的那个班次的userid，并添加加班记录
        Work work = workMapping.selectWorkByInfo(leave1.getUserId(), leave1.getLeaveStartTime().split(" ")[1], leave1.getLeaveEndTime().split(" ")[1], week + "");
        Work work1 = new Work(work.getId(), work.getUserId(), work.getStartTime(),work.getEndTime(),work.getWeek());
        work.setUserId(id);
        workMapping.updateWorkByPre(work1,work);
        More more = new More(null,id,leave1.getLeaveStartTime(),leave1.getLeaveEndTime(),null);
        moreMapping.inserMore(more);

    }

    @Override
    public List<Leave> getAllLeaveSev() throws ParseException {
        return utile.getLeaveBetweenTime(7);
    }

    @Override
    public List<Leave> getAllLeaveMon() throws ParseException {
        return utile.getLeaveBetweenTime(30);
    }

    @Override
    public List<Leave> getAllLeaveAll() {
        return leaveMapping.selectAllLeave();
    }
}
